var jwt = require('jsonwebtoken');
var constant = require('../private/constant')


class Util{
    constructor() {}

    static errorRespond (errorInfo, e, res){
        res.json({
            success: false,
            errorInfo: errorInfo,
            errorVerbose: e
        })
    }

    static dataRespond (body, res){
        res.json({
            success: true,
            data:{
                body: body,
                length: (body instanceof Array)? body.length : 1
            }
        })
    }

    static parseToken(req){
        return req.headers.authorization.replace("Bearer ", "");
    }

    static jwtVerify(req, res, callback){
        let token = this.parseToken(req)
        jwt.verify(token, constant.secretKey, (err, decoded)=>{
            if(err){
                this.errorRespond('JWT_VERIFY_ERROR', err, res)
            }else{
                callback(decoded)
            }
        })

    }

}

module.exports = Util